﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace visu
{
   public abstract class BaseService<T> where T:class,new()
    {
       public DALSession DalSession
       {
           get
           {
              // return new DBSession();//暂时
               return DALSession.Current;
           }
       }

       public BaseDAL<T> CurrentDal { get; set; }
       public abstract void SetCurrentDal();
       public BaseService()
       {
           SetCurrentDal();//子类一定要实现抽象方法。
       }


       #region 1.0 新增 实体 +int Add(T model)
       /// <summary>
       /// 新增 实体
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public bool Add(T model)
       {

            CurrentDal.Add(model);
            return DalSession.Savechanges();
       }
       #endregion


       #region 1.1新增 实体 +int Add(T model) 返回自增主键Id
       /// <summary>
       /// 新增 实体
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public string AddModelReturnId(T model, params string[] param)
       {
           return CurrentDal.AddModelReturnId(model, param);
       }
       #endregion


       #region 1.2 新增 实体，保存后，直接返回该实体 +int Add(T model)
       /// <summary>
       /// 新增 实体
       /// </summary>
       /// <param name="model"></param>
       /// <returns></returns>
       public T AddReturnModel(T model)
       {

           return CurrentDal.AddReturnModel(model);
           
       }
       #endregion


       #region 2.0 根据 id 删除 +int Del(T model)
       /// <summary>
       /// 根据 id 删除
       /// </summary>
       /// <param name="model">包含要删除id的对象</param>
       /// <returns></returns>
       public bool Del(T model)
       {
       
            CurrentDal.Del( model);
            return DalSession.Savechanges();
       }
       #endregion

       #region 3.0 根据条件删除 +int DelBy(Expression<Func<T, bool>> delWhere)
       /// <summary>
       /// 3.0 根据条件删除
       /// </summary>
       /// <param name="delWhere"></param>
       /// <returns></returns>
       public bool DelBy(Expression<Func<T, bool>> delWhere)
       {
            CurrentDal.DelBy(delWhere);
            return DalSession.Savechanges();
       }
       #endregion

       #region 4.0 修改 +int Modify(T model, params string[] proNames)
       /// <summary>
       /// 4.0 修改，如：
       /// T u = new T() { uId = 1, uLoginName = "asdfasdf" };
       /// this.Modify(u, "uLoginName");
       /// </summary>
       /// <param name="model">要修改的实体对象</param>
       /// <param name="proNames">要修改的 属性 名称</param>
       /// <returns></returns>
       public bool Modify(T model, params string[] proNames)
       {
            CurrentDal.Modify(model, proNames);
            return DalSession.Savechanges();
       }
       #endregion


       #region 4.0 修改 +int ModifyAllModel(T model)
       /// <summary>
       /// 4.0 修改，如：
       /// 需要先查询再修改
       /// </summary>
       /// <param name="model">要修改的实体对象</param>
       /// <param name="proNames">要修改的 属性 名称</param>
       /// <returns></returns>
       public bool ModifyAllModel(T model)
       {
            CurrentDal.ModifyAllModel(model);
            return DalSession.Savechanges();
       }
       #endregion


       #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
       /// <summary>
       /// 4.0 批量修改
       /// </summary>
       /// <param name="model">要修改的实体对象</param>
       /// <param name="whereLambda">查询条件</param>
       /// <param name="proNames">要修改的 属性 名称</param>
       /// <returns></returns>
       public bool ModifyBy(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames)
       {
            CurrentDal.ModifyBy(model, whereLambda, modifiedProNames);
          return  DalSession.Savechanges();
       }
       #endregion

       #region 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
       /// <summary>
       /// 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda)
       /// </summary>
       /// <param name="whereLambda"></param>
       /// <returns></returns>
       public List<T> GetListBy(Expression<Func<T, bool>> whereLambda)
       {
           return CurrentDal.GetListBy(whereLambda);
       }
       #endregion

       #region 5.1 根据条件 排序 和查询 + List<T> GetListBy<TKey>
       /// <summary>
       /// 5.1 根据条件 排序 和查询
       /// </summary>
       /// <typeparam name="TKey">排序字段类型</typeparam>
       /// <param name="whereLambda">查询条件 lambda表达式</param>
       /// <param name="orderLambda">排序条件 lambda表达式</param>
       /// <returns></returns>
       public List<T> GetListBy<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderLambda, bool IsAsc)
       {
           return CurrentDal.GetListBy(whereLambda, orderLambda, IsAsc);
       }
       #endregion

       #region 5.2 无条件查询 +List<T> GetListBy()
       /// <summary>
       /// 5.2 无条件查询 +List<T> GetListBy()
       /// </summary>
       /// <returns></returns>
       public List<T> GetListBy()
       {
           return CurrentDal.GetListBy();
       }
       #endregion

       #region 6.0 分页查询 + List<T> GetPagedList<TKey>
       /// <summary>
       /// 6.0 分页查询 + List<T> GetPagedList<TKey>
       /// </summary>
       /// <param name="pageIndex">页码</param>
       /// <param name="pageSize">页容量</param>
       /// <param name="whereLambda">条件 lambda表达式</param>
       /// <param name="orderBy">排序 lambda表达式</param>
       /// <returns></returns>
       public List<T> GetPagedList(int pageIndex, int pageSize, Expression<Func<T, bool>> whereLambda, string sort, string order)
       {
           return CurrentDal.GetPagedList(pageIndex, pageSize, whereLambda, sort,order);
       }
       #endregion

       #region 6.1分页查询 带输出 +List<T> GetPagedList<TKey>
       /// <summary>
       /// 6.1分页查询 带输出
       /// </summary>
       /// <typeparam name="TKey"></typeparam>
       /// <param name="pageIndex"></param>
       /// <param name="pageSize"></param>
       /// <param name="rowCount"></param>
       /// <param name="whereLambda"></param>
       /// <param name="orderBy"></param>
       /// <param name="isAsc"></param>
       /// <returns></returns>
       public List<T> GetPagedList(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, bool>> orderByProperty, bool IsAsc)
       {
           return CurrentDal.GetPagedList(pageIndex, pageSize, ref rowCount, whereLambda, orderByProperty, IsAsc);
       }
       #endregion

       #region 7.0查询数据条数+ public int GetCount(Expression<Func<T, bool>> whereLambda)
       /// <summary>
       /// 查询数据条数
       /// </summary>
       /// <param name="whereLambda"></param>
       /// <returns></returns>
       public int GetCount(Expression<Func<T, bool>> whereLambda)
       {
           return CurrentDal.GetCount(whereLambda);
       }
       #endregion

    }
}
